package com.sunda.spmsweb.overseacontroller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.OverseaDnConfirmDTO;
import com.sunda.spmsoversea.dto.OverseaDnSapDTO;
import com.sunda.spmsoversea.dto.OverseaDnSearchDTO;
import com.sunda.spmsoversea.dto.OverseaDnUpdateDTO;
import com.sunda.spmsoversea.service.IDataFromSapService;
import com.sunda.spmsoversea.service.IOverseaDnService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 海外仓交货单表(空运人带/转储/本地采购) 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-12-14
 */
@RestController
@RequestMapping("/overseaDn")
@Api(tags = "海外仓交货单表(空运人带/转储/本地采购)", description = "海外仓交货单表(空运人带/转储/本地采购)")
public class OverseaDnController {

    @Autowired
    IOverseaDnService iOverseaDnService;

    @Autowired
    IDataFromSapService iDataFromSapService;

    @RequestMapping("/updateOverseaDnRest")
    @ApiOperation(value = "从SAP实时获取转储交货单刷新", notes = "从SAP实时获取转储交货单(空运人带/转储/本地采购)\n" +
            "从SAP实时获取交货单采购订单等信息，businessType必输，其余字段可选，示例参数：\n" +
            "   sapDeliveryNote = 0700442993；\n" +
            "   businessType = 01；\n" +
            "   businessType - 业务标识\n" +
            "      01——人带、空运\n" +
            "      02——本地采购\n" +
            "      03——海外转储\n" +
            "      05——退货订单(海外仓不适用);\n" +
            "   werks = CI01;\n" +
            "   purchaseOrder = 4200024726 采购订单号;\n" +
            "   deliveryDateS = 2021-10-01 交货日期开始;\n" +
            "   deliveryDateE = 2021-10-20 交货日期结束;\n", httpMethod = "POST")
    public ResponseResult updateOverseaDnRest(String sapDeliveryNote,
                                              String purchaseOrder,
                                              @RequestParam String businessType,
                                              String deliveryDateS,
                                              String deliveryDateE,
                                              String werks){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iDataFromSapService.updateOverseaDnRest(sapDeliveryNote, businessType, purchaseOrder, deliveryDateS, deliveryDateE, werks, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaDnList")
    @ApiOperation(value = "查询转储交货单表头分页", notes = "查询转储交货单表头分页\n" +
            "businessType - 业务标识: 01——人带、空运; 02——本地采购; 03——海外转储; 05——退货订单(海外仓不适用);\n" +
            "示例参数如下：\n" +
            "{\n" +
            "\t\"sapDeliveryNote\": \"0700442993\",\n" +
            "\t\"spmsStatus\": \"\",\n" +
            "\t\"werksDelivery\": \"CN01\",\n" +
            "\t\"werksReceive\": \"CI01\",\n" +
            "\t\"deliveryDateBegin\": \"2021-10-01\",\n" +
            "\t\"deliveryDateEnd\": \"2021-10-01\",\n" +
            "\t\"businessType\": \"01\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"size\": 20\n" +
            "}", httpMethod = "POST")
    public ResponseResult getOverseaDeliveryNote(@RequestBody OverseaDnSearchDTO overseaDnSearchDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            IPage iPage = iOverseaDnService.getDnPage(overseaDnSearchDTO);
            return ResponseResult.success().add("overseaDnList", iPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOverseaDnDtlList")
    @ApiOperation(value = "查询转储交货单明细数据", notes = "查询转储交货单明细数据\n" +
            "示例参数：0700442993", httpMethod = "POST")
    public ResponseResult getOverseaDnDtlList(@RequestParam String sapDeliveryNote){
        try {
            return ResponseResult.success().add("overseaDnDtlList", iOverseaDnService.getDnDtlList(sapDeliveryNote));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/confirmArrival")
    @ApiOperation(value = "转储交货单确认到货", notes = "转储交货单确认到货\n" +
            "示例参数：sapDeliveryNote = 0700442993, sapPurchaseOrder = 4200024726, " +
            "dataVersion = 4521, whsLocationCodeReceive = 1001", httpMethod = "POST")
    public ResponseResult confirmArrival(@RequestBody OverseaDnConfirmDTO overseaDnConfirmDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaDnService.confirmArrival(overseaDnConfirmDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/updateDn")
    @ApiOperation(value = "转储交货单更新", notes = "转储交货单更新\n" +
            "交货单 保存 1-1、入库完成/提交审批 1-2、审批通过 2-3(SPMS按实收入库加库存) \n" +
            "", httpMethod = "POST")
    public ResponseResult updateDn(@RequestBody OverseaDnUpdateDTO overseaDnUpdateDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaDnService.updateDn(overseaDnUpdateDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/closeDn")
    @ApiOperation(value = "转储交货单关闭", notes = "转储交货单关闭\n" +
            "示例参数：sapDeliveryNote = 0700442993, dataVersion = 4521\n" +
            "状态变化：0-10；1-10；", httpMethod = "POST")
    public ResponseResult closeDn(@RequestParam String sapDeliveryNote, @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaDnService.closeDn(sapDeliveryNote, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/submitDnSap")
    @ApiOperation(value = "转储交货单提交SAP", notes = "转储交货单提交SAP\n" +
            "SPMS收货单据状态(默认0待到货;1入库中/审批不通过;2入库完成/待审批;3审批通过;4按应收提交SAP成功/SPMS按实收入库存;5按应收提交SAP失败;" +
            "6收货少收提交SAP失败;7收货少收提交SAP成功;8提交SAP完成;9收货完成;10关闭/废单;)\n" +
            "businessType - 业务标识: 01——人带、空运; 02——本地采购; 03——海外转储; " +
            "02——本地采购，提交SAP时，按实收提交SAP即可，多收少收不单独提交SAP；不需要成本中心字段；\n" +
            "本地采购提交SAP单独一套逻辑，按实收提交，无多收少收：\n" +
            "\t3审批通过/5按实收提交SAP失败\n" +
            "\t\t提交SAP —— 按实收数量提交SAP\n" +
            "\t\t\t失败 —— 审批通过后点击提交SAP失败 3-5/5-5；\n" +
            "\t\t\t成功 —— 3-8-9/5-8-9；\n" +
            "\n" +
            "01——人带、空运; 03——海外转储，交货单提交SAP，状态控制：\n" +
            "\t3审批通过/5按应收提交SAP失败\n" +
            "\t\t提交SAP —— 按应收数量提交SAP\n" +
            "\t\t\t失败 —— 审批通过后点击提交SAP失败 3-5/5-5；\n" +
            "\t\t\t成功 \n" +
            "\t\t\t\ti.不存在少收 \n" +
            "\t\t\t\t\t3-4-8-9/5-4-8-9；\n" +
            "\t\t\t\tii.存在少收，系统自动提交少收\n" +
            "\t\t\t\t\t少收提交成功 3-4-7-8-9/5-4-7-8-9；\n" +
            "\t\t\t\t\t少收提交失败 3-4-6/5-4-6；\n" +
            "\n" +
            "\t4按应收提交SAP成功/6收货少收提交SAP失败\n" +
            "\t\t提交SAP\n" +
            "\t\t\ti.不存在少收\n" +
            "\t\t\t\t4-8-9；\n" +
            "\t\t\tii.存在少收，则将少收信息重新提交SAP\n" +
            "\t\t\t\t成功 4-7-8-9/6-7-8-9；\n" +
            "\t\t\t\t失败 4-6/6-6；\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"dataVersion\": 4821,\n" +
            "\t\"postingDate\": \"2021-11-01\",\n" +
            "\t\"sapDeliveryNote\": \"0700442993\",\n" +
            "\t\"underPostingDate\": \"2021-11-01\",\n" +
            "\t\"costCenter\": \"CI0100143\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult submitDnSap(@RequestBody OverseaDnSapDTO overseaDnSapDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaDnService.submitDnSap(overseaDnSapDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/cancelDn")
    @ApiOperation(value = "转储交货单撤销", notes = "转储交货单撤销\n" +
            "转储交货单撤销，可操作状态及处理逻辑：\n" +
            "    0待到货;\n" +
            "    1入库中/审批不通过;\n" +
            "\t撤回 —— 撤回到待到货 1-0；\n" +
            "    2入库完成/待审批;\n" +
            "\t审批不通过 —— 退回到入库中 2-1；\n" +
            "    3审批通过/SPMS按实收入库存;\n" +
            "\t撤回 —— 撤回到入库中，SPMS按实收撤回入库减库存 3-1；\n" +
            "    4按应收提交SAP成功;\n" +
            "\t撤回 —— 按应收提交的入库凭证从SAP撤销\n" +
            "\t\t撤回成功 4-3；\n" +
            "\t\t撤回失败 4-4；\n" +
            "    5按应收提交SAP失败;\n" +
            "\t撤回 —— 撤回到入库中，SPMS按实收撤回入库减库存 5-1；\n" +
            "    6收货少收提交SAP失败;\n" +
            "\t撤回 —— 按应收提交的入库凭证从SAP撤销\n" +
            "\t\t撤回成功 6-3；\n" +
            "\t\t撤回失败 6-6；\n" +
            "    7收货少收提交SAP成功; —— 中间状态\n" +
            "    8提交SAP完成; —— 中间状态\n" +
            "    9收货完成/完结状态;\n" +
            "\t撤销 —— 不存在少收，撤回则按应收入库凭证从SAP撤销\n" +
            "\t\t成功 9-3；\n" +
            "\t\t失败 9-9；\n" +
            "\t撤销 —— 存在少收，先从SAP撤回少收凭证\n" +
            "\t\t失败 9-9；\n" +
            "\t\t成功 9-4；\n" +
            "\t\t\t从SAP撤回按应收提交的入库凭证\n" +
            "\t\t\t\t成功 4-3；\n" +
            "\t\t\t\t失败 4-4；\n" +
            "    10关闭/废单;\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"sapDeliveryNote\": \"0700442993\",\n" +
            "\t\"cancelPostingDate\": \"2021-11-30\",\n" +
            "\t\"cancelUnderPostingDate\": \"2021-11-30\",\n" +
            "\t\"dataVersion\": 4825\n" +
            "}", httpMethod = "POST")
    public ResponseResult cancelDn(@RequestBody OverseaDnSapDTO overseaDnSapDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaDnService.cancelDn(overseaDnSapDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }



    @RequestMapping("/zOverseaDnDesign")
    @ApiOperation(value = "转储交货单功能设计——文档参考", notes = "交货单(空运人带/转储/本地采购)功能设计\n" +
            "\n" +
            "SPMS收货单据状态:\n" +
            "\t0待到货;\n" +
            "\t1入库中/审批不通过;\n" +
            "\t2入库完成/待审批;\n" +
            "\t3审批通过/SPMS按实收入库存;\n" +
            "\t4按应收提交SAP成功;\n" +
            "\t5按应收提交SAP失败;\n" +
            "\t6收货少收提交SAP失败;\n" +
            "\t7收货少收提交SAP成功; —— 中间状态\n" +
            "\t8提交SAP完成; —— 中间状态\n" +
            "\t9收货完成/完结状态;\n" +
            "\t10关闭/废单;\n" +
            "\n" +
            "各状态操作按钮处理及状态变化：\n" +
            "\t0待到货;\n" +
            "\t\t更新 —— 从SAP刷新交货单数据 0-0；\n" +
            "\t\t关闭 —— 废弃 0-10；\n" +
            "\t\t确认到货 —— 确认到货，数据不再从SAP刷新 0-1；\n" +
            "\n" +
            "\t1入库中/审批不通过;\n" +
            "\t\t撤回 —— 撤回到待到货 1-0；\n" +
            "\t\t关闭 —— 废弃 1-12；\n" +
            "\t\t入库完成 —— 入库完成，提交审批 1-2；\n" +
            "\n" +
            "\t2入库完成/待审批;\n" +
            "\t\t审批不通过 —— 退回到入库中 2-1；\n" +
            "\t\t审批通过 —— 审批通过，SPMS按实收入库加库存 2-3；\n" +
            "\n" +
            "\t3审批通过/SPMS按实收入库存;\n" +
            "\t\t撤回 —— 审批通过撤回到入库中，SPMS按实收撤回入库减库存 3-1；\n" +
            "\t\t提交SAP —— 按应收数量提交SAP\n" +
            "\t\t\t失败 —— 审批通过后点击提交SAP失败 3-5；\n" +
            "\t\t\t成功 \n" +
            "\t\t\t\ti.不存在少收 3-4-8-9；\n" +
            "\t\t\t\tii.存在少收\n" +
            "\t\t\t\t\t系统自动提交少收\n" +
            "\t\t\t\t\t\t少收提交成功 3-4-7-8-9；\n" +
            "\t\t\t\t\t\t少收提交失败 3-4-6；\n" +
            "\n" +
            "\t4按应收提交SAP成功;\n" +
            "\t\t撤回 —— 按应收提交的入库凭证从SAP撤销\n" +
            "\t\t\t撤回成功 4-3；\n" +
            "\t\t\t撤回失败 4-4；\n" +
            "\t\t提交SAP\n" +
            "\t\t\t不存在少收\n" +
            "\t\t\t\t4-8-9；\n" +
            "\t\t\t存在少收，则将少收信息提交SAP\n" +
            "\t\t\t\t成功 4-7-8-9；\n" +
            "\t\t\t\t失败 4-6；\n" +
            "\n" +
            "\t5按应收提交SAP失败;\n" +
            "\t\t操作同状态3\n" +
            "\n" +
            "\t6收货少收提交SAP失败;\n" +
            "\t\t撤回 —— 按应收提交的入库凭证从SAP撤销\n" +
            "\t\t\t撤回成功 6-3；\n" +
            "\t\t\t撤回失败 6-6；\n" +
            "\t\t提交SAP —— 少收信息重新提交SAP\n" +
            "\t\t\t成功 6-7-8-9；\n" +
            "\t\t\t失败 6-6；\n" +
            "\n" +
            "\t7收货少收提交SAP成功; —— 中间状态\n" +
            "\n" +
            "\t8提交SAP完成; —— 中间状态\n" +
            "\t\n" +
            "\t9收货完成;\n" +
            "\t\t撤回\n" +
            "\t\t\t不存在少收，则按应收入库凭证从SAP撤销\n" +
            "\t\t\t\t成功 9-3；\n" +
            "\t\t\t\t失败 9-9；\n" +
            "\t\t\t存在少收，先从SAP撤回少收凭证\n" +
            "\t\t\t\t成功 \n" +
            "\t\t\t\t\t从SAP撤回按应收提交的入库凭证\n" +
            "\t\t\t\t\t\t成功 9-4-3；\n" +
            "\t\t\t\t\t\t失败 9-4；\n" +
            "\t\t\t\t失败 9-9\n" +
            "\n" +
            "\t10关闭/废单;",
            httpMethod = "POST")
    public ResponseResult overseaDnDesign(){
        try {
            return ResponseResult.success("文档设计接口");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
}
